package sapling.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sapling.dao.GrowthRecordDao;
import sapling.dao.TreeDao;
import sapling.dao.TreeWellDao;
import sapling.model.GrowthRecord;
import sapling.model.Tree;
import sapling.model.TreeWell;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2017/6/21.
 */
@Service
@Transactional
public class TreeWellService {

    @Autowired
    private TreeWellDao treeWellDao;
    @Autowired
    private TreeDao treeDao;
    @Autowired
    private GrowthRecordDao growthRecordDao;

    /**
     * 苗木入库
     * @param treeWell_id
     * @param treeSpecies
     * @param chestBreadth
     * @param crownWidth
     * @param height
     * @param clump
     * @param file
     * @return
     */
    public Tree saveTreeWell(Long treeWell_id, String treeSpecies, double chestBreadth, double chestBreadthmax, double crownWidth,double crownWidthmax,
                             double height,double heightmax,  Boolean clump, int number, String username, int salenumber,int salenumbermax, String serid,String remark, String file ,double cost) {
        TreeWell treeWell =treeWellDao.findOne(treeWell_id);
        Tree treesave=null;
        if(Objects.nonNull(treeWell)){
//            List<Tree> tree1=treeDao.findtree(treeWell_id);
//           if(Objects.nonNull(tree1)&&tree1.size()>0){
//               for(Tree t:tree1) {
//                   t.setLandmassid(0);
//                   t.setTree_well_id(0);
//                   treeDao.save(t);
//               }
//           }
            Tree tree=new Tree();
            tree.setTreeSpecies(treeSpecies);
            tree.setChestBreadth(chestBreadth);
            tree.setChestBreadthmax(chestBreadthmax);
            tree.setCrownWidth(crownWidth);
            tree.setCrownWidthmax(crownWidthmax);
            tree.setHeight(height);
            tree.setHeightmax(heightmax);
            tree.setClump(clump);
            tree.setResponseState(0);
            tree.setUsername(username);
            tree.setSerialNumber(serid);
            tree.setPutNurseryTime(new Date());
            tree.setNumber(number);
            tree.setRemark(remark);
            tree.setLandmassid(treeWell.getLandMass().getLandMass_id());
            tree.setSalenumber(salenumber);
            tree.setSalenumbermax(salenumbermax);
            tree.setImgurl(file);
            tree.setGather(false);
            tree.setPayment(false);
            tree.setCost(cost);
            treesave = treeDao.saveAndFlush(tree);
            treeWell.setTree_id(treesave.getTree_id());
            treeWell.setUseState(1);
            TreeWell saveTreewell = treeWellDao.saveAndFlush(treeWell);
            treesave.setTree_well_id(saveTreewell.getTreeWell_id());
            tree.setSename(saveTreewell.getLandMass().getAreaLocation().getSerialNumber()+saveTreewell.getLandMass().getSerialNumber()+"-"+saveTreewell.getSerialNumber());
            treeDao.saveAndFlush(treesave);
            record(treesave.getTree_id(),treeSpecies,height,heightmax,chestBreadth,chestBreadthmax,crownWidth,crownWidthmax,salenumber,salenumbermax,file,cost);
        }
        return treesave;
    }

    public Tree updateTree(Long tree_id, String treeSpecies, double chestBreadth, double chestBreadthmax, double crownWidth, double crownWidthmax,
                           double height, double heightmax,Boolean clump,String username, int salenumber,int salenumbermax, String imgUrl,double cost) {
        Tree treesave=new Tree();
        if(tree_id!=0);
        Tree tree =treeDao.findOne(tree_id);
        if(Objects.nonNull(tree)){
            tree.setTreeSpecies(treeSpecies);
            tree.setChestBreadth(chestBreadth);
            tree.setChestBreadthmax(chestBreadthmax);
            tree.setCrownWidth(crownWidth);
            tree.setCrownWidthmax(crownWidthmax);
            tree.setHeight(height);
            tree.setHeightmax(heightmax);
            tree.setClump(clump);
            tree.setUsername(username);
            tree.setSalenumber(salenumber);
            tree.setSalenumbermax(salenumbermax);
            tree.setImgurl(imgUrl);
            tree.setCost(cost);
            treesave= treeDao.saveAndFlush(tree);
            record(treesave.getTree_id(),treeSpecies,height,heightmax,chestBreadth,chestBreadthmax,crownWidth,crownWidthmax,salenumber,salenumbermax,imgUrl,cost);
        }
        return treesave;
    }

    public void record(Long tree_id,String treeSpecies,double height,double heightmax,double chestBreadth,double chestBreadthmax,
                       double crownWidth,double crownWidthmax,int salenumber,int salenumbermax,String imgurl,double cost){
       if(Objects.nonNull(tree_id)){
           Tree tree=treeDao.findOne(tree_id);
           GrowthRecord growthRecord=new GrowthRecord();
           growthRecord.setTreeSpecies(treeSpecies);
           growthRecord.setHeight(height);
           growthRecord.setHeightmax(heightmax);
           growthRecord.setChestBreadth(chestBreadth);
           growthRecord.setChestBreadthmax(chestBreadthmax);
           growthRecord.setCrownWidth(crownWidth);
           growthRecord.setCrownWidthmax(crownWidthmax);
           growthRecord.setSalenumber(salenumber);
           growthRecord.setSalenumbermax(salenumbermax);
           growthRecord.setRecordTime(new Date());
           growthRecord.setImgurl(imgurl);
           growthRecord.setCost(cost);
           growthRecord.setTree(tree);
           growthRecordDao.saveAndFlush(growthRecord);
       }
    }
    public void out(long treeid){
        TreeWell treeWell = treeWellDao.findbytree(treeid);
        if(treeWell!=null) {
            treeWell.setUseState(0);
            treeWell.setTree_id(0);
        }
    }
}
